package GUI;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import regelinterpreter02.Logic;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegelinterpreterGUI.java
 *
 * Created on 20.11.2011, 13:59:11
 */

/**
 *
 * @author Daniel & Patrick
 */
public class RegelinterpreterGUI extends javax.swing.JFrame implements  ItemListener {
    
    List<String> diagnosis = new ArrayList<String>();
    List<String> answers = new ArrayList<String>();
    private int ind = -1;
    private Logic logic = new Logic();

    /** Creates new form RegelinterpreterGUI */
    public RegelinterpreterGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_Question = new javax.swing.JLabel();
        jButton_next = new javax.swing.JButton();
        jButton_finished = new javax.swing.JButton();
        jLabel_Answer = new javax.swing.JLabel();
        jLabel_Image = new javax.swing.JLabel();
        jLabel_Module = new javax.swing.JLabel();
        jComboBox_Selection = new javax.swing.JComboBox();
        jLabel_Select = new javax.swing.JLabel();
        jLabel_Answer2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem_new = new javax.swing.JMenuItem();
        jMenuItem_Quit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem_Credits = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Regelinterpreter");
        setBackground(new java.awt.Color(255, 255, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(java.awt.Color.white);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel_Question.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel_Question.setText("Frage");
        getContentPane().add(jLabel_Question, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 520, 20));

        jButton_next.setText("Weiter");
        jButton_next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_nextActionPerformed(evt);
            }
        });
        getContentPane().add(jButton_next, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, 80, 30));
        jButton_next.setVisible(false);

        jButton_finished.setText("Fertig");
        jButton_finished.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_finishedActionPerformed(evt);
            }
        });
        getContentPane().add(jButton_finished, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 330, 90, 30));
        jButton_finished.setVisible(false);
        getContentPane().add(jLabel_Answer, new org.netbeans.lib.awtextra.AbsoluteConstraints(313, 173, -1, -1));

        jLabel_Image.setIcon(new javax.swing.ImageIcon("pictures/drehtisch.jpg"));
        jLabel_Image.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(jLabel_Image, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 90, 263, 200));

        jLabel_Module.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel_Module.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel_Module.setText("Modulname");
        getContentPane().add(jLabel_Module, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 520, 20));

        jComboBox_Selection.setToolTipText("Antwort wählen");
        jComboBox_Selection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_SelectionActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox_Selection, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, 250, 20));

        jLabel_Select.setText("Bitte wählen Sie eine Antwort");
        getContentPane().add(jLabel_Select, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, 250, -1));

        jLabel_Answer2.setText("Diagnose");
        getContentPane().add(jLabel_Answer2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 300, 540, -1));

        jLabel1.setText("    ");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 350, -1, 20));
        getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 30, 590, 10));
        getContentPane().add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 320, 590, 10));
        getContentPane().add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 70, 590, 10));

        jMenu1.setText("Datei");

        jMenuItem_new.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        jMenuItem_new.setText("Neu");
        jMenuItem_new.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_newActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem_new);

        jMenuItem_Quit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem_Quit.setText("Beenden");
        jMenuItem_Quit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_QuitActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem_Quit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("?");

        jMenuItem_Credits.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem_Credits.setText("Credits");
        jMenuItem_Credits.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_CreditsActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem_Credits);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem_CreditsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem_CreditsActionPerformed
        JOptionPane.showMessageDialog(this, "© Patrick Krause und Daniel Laufer (2011)", "Credits", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jMenuItem_CreditsActionPerformed

    private void jMenuItem_QuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem_QuitActionPerformed
        int n = JOptionPane.showConfirmDialog(this, "Wollen Sie das Programm wirklich beenden?", "Beenden", JOptionPane.YES_NO_OPTION);
        if (n==0)System.exit(0);
    }//GEN-LAST:event_jMenuItem_QuitActionPerformed


    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        int n = JOptionPane.showConfirmDialog(this, "Wollen Sie das Programm wirklich beenden?", "Beenden", JOptionPane.YES_NO_OPTION);
        if (n==0)System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    private void jButton_nextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_nextActionPerformed
        jButton_next.setText("Weiter");
        jButton_finished.setVisible(false);
        jLabel_Module.setText("");
        jLabel_Question.setText("");
        jLabel_Answer2.setText("Diagnose");
        ind = -1;
        diagnosis.clear();
        answers.clear();
        setSelection(1);        //setzen der Antworten und Diagnosen auf leer
        if(logic.doNext()){     //erst weiter, wenn doNext() fertig und true zurueckliefert
            jButton_next.setVisible(logic.isjButton_next_setVisible());
            jLabel_Module.setText(logic.getjLabel_Module());
            jLabel_Question.setText(logic.getjLabel_Question());
            jLabel_Image.setIcon(new javax.swing.ImageIcon(logic.getjLabel_Image()));
            answers.addAll(logic.getAnswers());
            diagnosis.addAll(logic.getDiagnosis());
            setSelection(1);    //setzen der Antworten, Diagnosen und Bilder entsprechend der ermittelten Werte aus logic.java
        }
    }//GEN-LAST:event_jButton_nextActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated

    }//GEN-LAST:event_formWindowActivated

    private void jButton_finishedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_finishedActionPerformed
        int n = JOptionPane.showConfirmDialog(this, "Wollen Sie das Programm wirklich beenden?", "Beenden", JOptionPane.YES_NO_OPTION);
        if (n==0)System.exit(0);
    }//GEN-LAST:event_jButton_finishedActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowOpened

    private void jMenuItem_newActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem_newActionPerformed
        startFunction();
    }//GEN-LAST:event_jMenuItem_newActionPerformed

    private void jComboBox_SelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_SelectionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox_SelectionActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RegelinterpreterGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_finished;
    private javax.swing.JButton jButton_next;
    private javax.swing.JComboBox jComboBox_Selection;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel_Answer;
    private javax.swing.JLabel jLabel_Answer2;
    private javax.swing.JLabel jLabel_Image;
    private javax.swing.JLabel jLabel_Module;
    private javax.swing.JLabel jLabel_Question;
    private javax.swing.JLabel jLabel_Select;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem_Credits;
    private javax.swing.JMenuItem jMenuItem_Quit;
    private javax.swing.JMenuItem jMenuItem_new;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    // End of variables declaration//GEN-END:variables

    
    public void setQuestion(String text){   //setter fuer Label "Question" fuer Zugriff von aussen
        jLabel_Question.setSize(text.length(), 14);
        jLabel_Question.setText(text);
    }
    
    public void setSelection(int type){     //fuellen der Antworten, vorbereitet fuer weitere Regel-Formate
        jComboBox_Selection.removeAllItems();
        jComboBox_Selection.addItem("");
        for(int i = 0;i<answers.size();i++){
            jComboBox_Selection.addItem(answers.get(i));
        }
        switch(type){
			case 1: //1=StatementEqualsValue
                                break;
                        case 2: //2=StatementOnly
                                break;
                        case 3: //3=StatementEqualsStatement
                                break;
                        case 4: //4=StatementGreaterValue
                                break;
                        case 5: //5=StatementLessValue
                                break;
                        case 6: //6=StatementUnequalsValue
                                break;
                    }
    }
    
    public void itemStateChanged( ItemEvent event ){    //reagieren auf Aenderung des Antwortfeldes durch Nutzer
        if( event.getSource() == jComboBox_Selection    
                                && event.getStateChange() == ItemEvent.SELECTED){
            jComboBox_Selection.setSelectedItem(event);
            ind = jComboBox_Selection.getSelectedIndex();
            if(logic.useRule((String)jComboBox_Selection.getItemAt(ind))){  //ausloesen/feuern der Regel und Ausgabe des Ergebnisses
                jLabel_Answer2.setText(logic.getjLabel_Answer2());
                jButton_next.setVisible(logic.isjButton_next_setVisible());
                jButton_finished.setVisible(logic.jButton_finished_setVisible());
            }
        }
    }
    
    private void startFunction(){   //wird ausgeloest durch "Neu"-Auswahl; Ruecksetzen der Dialogkomponente
        logic.startFunction();
        jButton_next.setText("Start");
        jButton_next.setVisible(true);
        jButton_finished.setVisible(false);
        jLabel_Question.setText("Frage");
        jLabel_Answer2.setText("Diagnose");
        diagnosis.clear();
        answers.clear();
        setSelection(1);
        jComboBox_Selection.addItemListener(this);
        jLabel_Module.setText("Modulname");
        jLabel_Image.setIcon(new javax.swing.ImageIcon("pictures/drehtisch.jpg"));
    }

}
